package com.ogawa.a7517.fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.reslib.utils.ToastUtils;
import com.ogawa.a7517.Constant;
import com.ogawa.a7517.DataManager;
import com.ogawa.a7517.R;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;
import com.ogawa.a7517.bean.ProgramBean;
import com.ogawa.a7517.utils.ViewUtil;
import com.ogawa.a7517.widget.ArcSeekBar;
import com.ogawa.a7517.widget.GasView;
import com.ogawa.a7517.widget.PointView;
import com.ogawa.a7517.widget.TimeDialog;

import java.util.List;

public class AutoMassageFragment extends BaseFragment implements View.OnClickListener {
    private TextView tvBackHeat, tvKneeHeat, tvHandRoll, tvFootRoll, tvTime, tvTitle;
    private TextView tvGasLev, tv3DLev, tvKneadLev;
    private ArcSeekBar asbGas, asb3D, asbKnead;
    private ImageView ivGas, iv3D, ivKnead;
    private ImageView ivBackHeat, ivKneeHeat;
    private ImageView ivHandRollLeft, ivHandRollRight, ivFootRollLeft, ivFootRollRight;
    private TextView tvCollect, tvSpeed;
    private PointView pointView;
    private GasView gasView;
    private AnimatorSet footAnimatorSet;
    private AnimatorSet handAnimatorSet;
    private TimeDialog timeDialog;
    private int distance, running;
    private int[] imgStandardRes = {R.drawable.img_scale_standard_0, R.drawable.img_scale_standard_1
            , R.drawable.img_scale_standard_2, R.drawable.img_scale_standard_3, R.drawable.img_scale_standard_4};
    private int[] imgBigRes = {R.drawable.img_scale_big_1, R.drawable.img_scale_big_1, R.drawable.img_scale_big_2
            , R.drawable.img_scale_big_3, R.drawable.img_scale_big_4, R.drawable.img_scale_big_5};

    @Override
    public int getLayoutId() {
        return R.layout.fragment_auto_massage;
    }

    @Override
    public void initView(View view) {
//        TextView tvHeat = view.findViewById(R.id.tv_heat);
//        tvHeat.setOnClickListener(this);
//        tvHeat.post(() -> distance = tvHeat.getWidth() + ViewUtil.dip2px(5, activity7517));
//        TextView tvRoll = view.findViewById(R.id.tv_roll_wheel);
//        tvRoll.setOnClickListener(this);
        view.findViewById(R.id.tv_back).setOnClickListener(this);
        tvCollect = view.findViewById(R.id.tv_collect);
        tvCollect.setOnClickListener(this);
        pointView = view.findViewById(R.id.point_view);
        gasView = view.findViewById(R.id.gas_view);
        tvBackHeat = view.findViewById(R.id.tv_back_heat);
        tvBackHeat.setOnClickListener(this);
        tvKneeHeat = view.findViewById(R.id.tv_knee_heat);
        tvKneeHeat.setOnClickListener(this);
        tvHandRoll = view.findViewById(R.id.tv_hand_roll);
        tvHandRoll.setOnClickListener(this);
        tvFootRoll = view.findViewById(R.id.tv_foot_roll);
        tvFootRoll.setOnClickListener(this);
        tvGasLev = view.findViewById(R.id.tv_gas_lev_num);
        tv3DLev = view.findViewById(R.id.tv_3d_num);
        tvSpeed = view.findViewById(R.id.tv_speed);
        tvKneadLev = view.findViewById(R.id.tv_knead_speed_num);
        tvTime = view.findViewById(R.id.tv_time);
        tvTitle = view.findViewById(R.id.tv_title);
        asbGas = view.findViewById(R.id.asb_gas_lev);
        asb3D = view.findViewById(R.id.asb_3d_lev);
        asbKnead = view.findViewById(R.id.asb_knead_lev);
        ivGas = view.findViewById(R.id.iv_gas_lev);
        iv3D = view.findViewById(R.id.iv_3d_adjust);
        ivKnead = view.findViewById(R.id.iv_knead_speed);
        ivBackHeat = view.findViewById(R.id.iv_back_heat);
        ivKneeHeat = view.findViewById(R.id.iv_knee_heat);
        ivHandRollLeft = view.findViewById(R.id.iv_hand_roll_left);
        ivHandRollRight = view.findViewById(R.id.iv_hand_roll_right);
        ivFootRollLeft = view.findViewById(R.id.iv_foot_roll_left);
        ivFootRollRight = view.findViewById(R.id.iv_foot_roll_right);
        view.findViewById(R.id.iv_bg_time).setOnClickListener(this);
        initHandAnimator();//初始化手部滚轮动画
        initFootAnimator();//初始化脚底滚轮动画
        initAsbProgressChangeListener();
        deviceStateChange();
    }

    private void initAsbProgressChangeListener() {
        asbGas.setOnProgressChangeListener(new ArcSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(ArcSeekBar seekBar, int progress, boolean isUser) {

            }

            @Override
            public void onStartTrackingTouch(ArcSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(ArcSeekBar seekBar) {
                activity7517.publish(Constant.AIR_INTENSITY, "1", String.valueOf(seekBar.getProgress()));
            }
        });
        asb3D.setOnProgressChangeListener(new ArcSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(ArcSeekBar seekBar, int progress, boolean isUser) {

            }

            @Override
            public void onStartTrackingTouch(ArcSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(ArcSeekBar seekBar) {
                activity7517.publish(Constant._3D_STRENGTH_LEVEL, "1", String.valueOf(seekBar.getProgress()));
            }
        });
        asbKnead.setOnProgressChangeListener(new ArcSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(ArcSeekBar seekBar, int progress, boolean isUser) {

            }

            @Override
            public void onStartTrackingTouch(ArcSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(ArcSeekBar seekBar) {
                activity7517.publish(Constant.XYZ_SPEED_LEVEL, "1", String.valueOf(seekBar.getProgress()));
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle("");
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
     /*   if (id == R.id.tv_heat) {
            if (tvBackHeat.getVisibility() == View.GONE) {
                showAnim(tvBackHeat, distance);
                showAnim(tvKneeHeat, 2 * distance);
            } else {
                hideAnim(tvBackHeat, distance);
                hideAnim(tvKneeHeat, 2 * distance);
            }
        } else if (id == R.id.tv_roll_wheel) {
            if (tvHandRoll.getVisibility() == View.GONE) {
                showAnim(tvHandRoll, distance);
                showAnim(tvFootRoll, 2 * distance);
            } else {
                hideAnim(tvHandRoll, distance);
                hideAnim(tvFootRoll, 2 * distance);
            }
        } else*/
        if (id == R.id.tv_back) {
            activity7517.popBackStack();
        } else if (id == R.id.tv_back_heat) {
            activity7517.publish(Constant.HEAT_BACK, "3");
        } else if (id == R.id.tv_knee_heat) {
            activity7517.publish(Constant.KNEE_HEAT, "3");
        } else if (id == R.id.tv_hand_roll) {
            activity7517.publish(Constant.HAND_SCROLL, "3");
        } else if (id == R.id.tv_foot_roll) {
            activity7517.publish(Constant.FOOT_WHEEL, "3");
        } else if (id == R.id.iv_bg_time) {
            showTimeDialog();
        } else if (id == R.id.tv_collect) {
            List<Integer> collections = DataManager.getInst().getCollections();
            if (!tvCollect.isSelected()) {
                if (collections.size() == 10) {
                    ToastUtils.showShortToast(activity7517, getString(R.string.collect_full));
                    return;
                }
                collections.add(running);
            } else {
                collections.remove(Integer.valueOf(running));
            }
            tvCollect.setSelected(!tvCollect.isSelected());
            DataManager.getInst().setCollections(collections);
        }
    }

    private void showTimeDialog() {
        if (timeDialog == null) {
            timeDialog = new TimeDialog(activity7517);
        }
        timeDialog.show();
    }

    private void showAnim(View v, int instance) {
        ObjectAnimator showAnim = ObjectAnimator.ofFloat(v, "translationX", 0, instance);
        showAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                v.setVisibility(View.VISIBLE);
            }
        });
        showAnim.start();
    }

    private void hideAnim(View v, int instance) {
        ObjectAnimator showAnim = ObjectAnimator.ofFloat(v, "translationX", instance, 0);
        showAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                v.setVisibility(View.GONE);
            }
        });
        showAnim.start();
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (stateBean != null) {
            if (stateBean.getFunctions().getTimeRemaining() != null) {
                tvTime.setText(stateBean.getFunctions().getTimeRemaining().getModelValue());
            }
            if (stateBean.getFunctions().getAutoProgram() != null) {
                int auto = Integer.parseInt(stateBean.getFunctions().getAutoProgram().getModelValue());
                if (running != auto) {
                    running = auto;
                    tvCollect.setSelected(false);
                    if (auto > 16) {
                        switch (auto) {
                            case 17:
                                tvTitle.setText(R.string.area_shoulder);
                                break;
                            case 18:
                                tvTitle.setText(R.string.area_waist);
                                break;
                            case 19:
                                tvTitle.setText(R.string.area_hand);
                                break;
                            case 20:
                                tvTitle.setText(R.string.area_hip);
                                break;
                            case 21:
                                tvTitle.setText(R.string.area_leg);
                                break;
                        }
                        tvCollect.setVisibility(View.INVISIBLE);
                    } else {
                        tvCollect.setVisibility(View.VISIBLE);
                        for (ProgramBean programBean : DataManager.getInst().getProgramBeans()) {
                            if (programBean.getModel() == auto) {
                                tvTitle.setText(programBean.getName());
                                break;
                            }
                        }
                        List<Integer> collections = DataManager.getInst().getCollections();
                        for (Integer collection : collections) {
                            if (running == collection) {
                                tvCollect.setSelected(true);
                                break;
                            }
                        }
                    }
                }
            }
            if (stateBean.getFunctions().getAirIntensity() != null) {//充气强度
                int gasLev = Integer.parseInt(stateBean.getFunctions().getAirIntensity().getModelValue());
                tvGasLev.setText(String.valueOf(gasLev));
                asbGas.setProgress(gasLev);
                ivGas.setImageResource(imgStandardRes[Math.min(gasLev, 4)]);
            }
            if (stateBean.getFunctions().get_$3DStrengthLevel() != null) {//3D强度
                int _3DLev = Integer.parseInt(stateBean.getFunctions().get_$3DStrengthLevel().getModelValue());
                tv3DLev.setText(String.valueOf(_3DLev));
                asb3D.setProgress(_3DLev);
                iv3D.setImageResource(imgBigRes[Math.min(_3DLev, 5)]);
            }
            if (stateBean.getFunctions().getXyzSpeedLevel() != null) {//揉捏速度
                int kneadLev = Integer.parseInt(stateBean.getFunctions().getXyzSpeedLevel().getModelValue());
                tvKneadLev.setText(String.valueOf(kneadLev));
                asbKnead.setProgress(kneadLev);
                ivKnead.setImageResource(imgStandardRes[Math.min(kneadLev, 4)]);
            }
            if (stateBean.getFunctions().getHeatBack() != null) {//背部加热
                int heatBack = Integer.parseInt(stateBean.getFunctions().getHeatBack().getModelValue());
                tvBackHeat.setSelected(heatBack == 1);
                ivBackHeat.setVisibility(heatBack == 1 ? View.VISIBLE : View.GONE);
            }
            if (stateBean.getFunctions().getKneeHeat() != null) {//膝部加热
                int kneeHeat = Integer.parseInt(stateBean.getFunctions().getKneeHeat().getModelValue());
                tvKneeHeat.setSelected(kneeHeat == 1);
                ivKneeHeat.setVisibility(kneeHeat == 1 ? View.VISIBLE : View.GONE);
            }
            if (stateBean.getFunctions().getHandScroll() != null) {//手部滚轮
                int handScroll = Integer.parseInt(stateBean.getFunctions().getHandScroll().getModelValue());
                ivHandRollLeft.setSelected(handScroll == 1);
                ivHandRollRight.setSelected(handScroll == 1);
                if (handScroll == 1) {
                    if (!handAnimatorSet.isRunning()) {
                        handAnimatorSet.start();
                    }
                } else {
                    handAnimatorSet.cancel();
                }
            }
            if (stateBean.getFunctions().getHandScrollButton() != null) {
                int handScrollButton = Integer.parseInt(stateBean.getFunctions().getHandScrollButton().getModelValue());
                tvHandRoll.setSelected(handScrollButton == 1);
            }
            if (stateBean.getFunctions().getFootWheel() != null) {//脚底滚轮
                int footWheel = Integer.parseInt(stateBean.getFunctions().getFootWheel().getModelValue());
                ivFootRollLeft.setSelected(footWheel == 1);
                ivFootRollRight.setSelected(footWheel == 1);
                if (footWheel == 1) {
                    if (!footAnimatorSet.isRunning()) {
                        footAnimatorSet.start();
                    }
                } else {
                    footAnimatorSet.cancel();
                }
            }
            if (stateBean.getFunctions().getFootWheelButton() != null) {
                int footWheelButton = Integer.parseInt(stateBean.getFunctions().getFootWheelButton().getModelValue());
                tvFootRoll.setSelected(footWheelButton == 1);
            }
            DeviceStateBean.FunctionsBean.YPosBean yPos = stateBean.getFunctions().getYPos();
            if (yPos != null) {
                int y = Integer.parseInt(yPos.getModelValue());
                pointView.setPointVis(y);
            }
            gasView.deviceStateChange();
        }
    }

    private void initFootAnimator() {
        footAnimatorSet = new AnimatorSet();
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(ivFootRollLeft, "rotation", 0, 720);
        ObjectAnimator rotate2Anim = ObjectAnimator.ofFloat(ivFootRollRight, "rotation", 0, 720);
        rotateAnim.setRepeatCount(ValueAnimator.INFINITE);
        rotate2Anim.setRepeatCount(ValueAnimator.INFINITE);
        rotateAnim.setRepeatMode(ValueAnimator.RESTART);
        rotate2Anim.setRepeatMode(ValueAnimator.RESTART);
        footAnimatorSet.setDuration(1000);
        footAnimatorSet.play(rotateAnim).with(rotate2Anim);
    }

    private void initHandAnimator() {
        handAnimatorSet = new AnimatorSet();
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(ivHandRollLeft, "rotation", 0, 720);
        ObjectAnimator rotate2Anim = ObjectAnimator.ofFloat(ivHandRollRight, "rotation", 0, 720);
        rotateAnim.setRepeatCount(ValueAnimator.INFINITE);
        rotate2Anim.setRepeatCount(ValueAnimator.INFINITE);
        rotateAnim.setRepeatMode(ValueAnimator.RESTART);
        rotate2Anim.setRepeatMode(ValueAnimator.RESTART);
        handAnimatorSet.setDuration(1000);
        handAnimatorSet.play(rotateAnim).with(rotate2Anim);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (footAnimatorSet != null) {
            footAnimatorSet.cancel();
        }
        if (handAnimatorSet != null) {
            handAnimatorSet.cancel();
        }
    }
}
